package com.groep21.android;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;

public class TakePhoto extends Activity {
	private PhotoDbAdapter mDbHelper;
	
	protected ImageView iv = null;
	protected Bitmap b = null;
	protected Uri ImgUri;
	
	private EditText lengthval;
	private EditText weightval;
	private EditText descriptionval;
	
	final int IMAGE_CAPTURE = 1;
	final int IMAGE_BROWSE = 2;
	
	// private Context ctx;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.take_photo);
        Log.i(TakePhoto.class.getName(),"Activity Ready to Go!");
        
        
        
        final Button  btncam = (Button) this.findViewById(R.id.TakePhoto);
        final Button  btnbrowse = (Button) this.findViewById(R.id.BrowsePhoto);
        final Button  btnok = (Button) this.findViewById(R.id.ConfirmPhoto);
        lengthval = (EditText) this.findViewById(R.id.txtLength);
        weightval = (EditText) this.findViewById(R.id.txtWeight);
        descriptionval = (EditText) this.findViewById(R.id.txtDescription);
        
        btncam.setOnClickListener(new View.OnClickListener(){

        	public void onClick(View v){
        		Log.i(TakePhoto.class.getName(),"Button Clicked!");
        		try {
        			Intent action = new Intent("android.media.action.IMAGE_CAPTURE");
        			action.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
        			startActivityForResult(action, IMAGE_CAPTURE);
        		} catch (Exception e) {
        			Log.e(PhotoBooth.class.getName(),"Error occured [" + e.getMessage() + "]");
        		}
        	}
        });
        
        btnbrowse.setOnClickListener(new View.OnClickListener(){

        	public void onClick(View v){
        		Log.i(TakePhoto.class.getName(),"Button Clicked!");
        		try {
        			Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        			startActivityForResult(i, IMAGE_BROWSE);
        		} catch (Exception e) {
        			Log.e(PhotoBooth.class.getName(),"Error occured [" + e.getMessage() + "]");
        		}
        	}
        });
        
        btnok.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            	float lengte = new Float(lengthval.getText().toString());
            	float gewicht = new Float(weightval.getText().toString());
            	String fragment = descriptionval.getText().toString();
            	Integer fotoindex;
            	String title;
            	
            	if (b != null) {
            		if (ImgUri == null) {
            		
	            		// Database openen en laatste fotoid ophalen en daar filenaam uit bakken
	            		mDbHelper = new PhotoDbAdapter(view.getContext());
	                    mDbHelper.open();
	            		
	            		fotoindex = new Integer(mDbHelper.lastPhotoId());
	    				fotoindex = fotoindex + 1;
	    				title = "BabyTracker" + fotoindex.toString();
	            		
	    				
	    				// Save the name and description of an image in a ContentValues map.  
	    		    	
	    				ImgUri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage( getContentResolver(), b,  title, fragment));
	    				Log.d(this.toString(), "Title: "
	    						+ title);
	    				Log.d(this.toString(), "Uri: "
	    						+ ImgUri.toString());
	    				
	    				// Bewaar de fotogegevens in de database            		
	    				
	    				mDbHelper.createPhoto(lengte, gewicht, fragment, ImgUri.toString());
	    				
	    				mDbHelper.close();
            		} else {
            			
            			mDbHelper = new PhotoDbAdapter(view.getContext());
	                    mDbHelper.open();
	                    mDbHelper.createPhoto(lengte, gewicht, fragment, ImgUri.toString());
	    				
	    				mDbHelper.close();
            			
            		}
            	}
            	
                setResult(RESULT_OK);
                finish();
            }

        });

        iv = (ImageView) this.findViewById(R.id.PictureFrame);

    }
    
    protected void onActivityResult(int requestCode,int resultCode,Intent data)
    {
    	

    	try {
    		switch(requestCode) {
    			case IMAGE_CAPTURE:
    				if (resultCode == RESULT_OK) {
    					if (b != null) b.recycle();
    					b = (Bitmap) data.getExtras().get("data");
    					if (b != null) {
    						iv.setImageBitmap(b);
    					}
    				}
    			case IMAGE_BROWSE:
    				if(resultCode == RESULT_OK){  

    		            ImgUri = data.getData();  
  
    		            b = MediaStore.Images.Media.getBitmap(getContentResolver(), ImgUri);
    		            iv.setImageBitmap(b);
    				}
    		}
    	} catch (Exception e) {
    		Log.e(TakePhoto.class.getName(),"onActivityResult Errorshit [" + e.getMessage() + "]");
    	}

    }
}